package org.codefinger.dao.impl;

import org.codefinger.dao.util.TLArrayList;
import org.codefinger.dao.util.TLStringBuilder;

public class SQLVisitor {

	private TLStringBuilder	sqlBuilder;

	private TLArrayList		params;

	private QueryParam		queryParam;

	public SQLVisitor(QueryParam queryParam) {
		super();
		this.queryParam = queryParam;
		this.sqlBuilder = new TLStringBuilder();
		this.params = new TLArrayList();
	}

	public void appendSQL(char c) {
		sqlBuilder.append(c);
	}

	public void appendSQL(String str) {
		sqlBuilder.append(str);
	}

	public int[] getOrders() {
		return queryParam.getOrders();
	}

	public int getSqlSize() {
		return sqlBuilder.length();
	}

	public String getSQL(){
		return sqlBuilder.toString();
	}

	public int getParamSize() {
		return params.size();
	}

	public Object[] getParams() {
		return params.toArray();
	}

	public void back(int sqlSize) {
		sqlBuilder.back(sqlSize);
	}

	public void back(int sqlSize, int paramSize) {
		sqlBuilder.back(sqlSize);
		params.back(paramSize);
	}

	public void close() {
		sqlBuilder.close();
		params.close();
	}

	public Object getParam(String name) {
		return queryParam.getParam(name);
	}

	public Object getParam(int index) {
		return queryParam.getParam(index);
	}

	public void addParamForced(String name) {
		params.add(queryParam.getParam(name));
	}

	public void addParamForced(int index) {
		params.add(queryParam.getParam(index));
	}

	public void addParam(Object value) {
		params.add(value);
	}

	public boolean matchParam(String name) {
		Object param = queryParam.getParam(name);
		if (param == null) {
			return false;
		}
		params.add(param);
		return true;
	}

	public boolean matchParam(int index) {
		Object param = queryParam.getParam(index);
		if (param == null) {
			return false;
		}
		params.add(param);
		return true;
	}

}
